package edu.hit.irlab.coref.features.test;

import java.util.List;

import nlpeap.ml.FeatureDescription;
import nlpeap.ml.FeatureType;
import nlpeap.tms.Token;
import edu.hit.irlab.coref.PairFeatureExtractor;
import edu.hit.irlab.coref.PairInstance;
import edu.hit.irlab.coref.mentionfinder.Mention;

public class WnZhang implements PairFeatureExtractor {

	public static final FeatureDescription<Boolean> FD_IS_STRMATCH=
        new FeatureDescription<Boolean>(FeatureType.FT_BOOL, "str_match");
	
	public static final FeatureDescription<Boolean> FD_IS_ONEPRO=
        new FeatureDescription<Boolean>(FeatureType.FT_BOOL, "one_pro");
	
	public static final FeatureDescription<Boolean> FD_ARE_BOTHPROANDMATCH=
        new FeatureDescription<Boolean>(FeatureType.FT_BOOL, "both_pro_match");
	
	public static final FeatureDescription<Boolean> FD_ARE_BOTHPROPER=
        new FeatureDescription<Boolean>(FeatureType.FT_BOOL, "both_proper");
	
	public static final FeatureDescription<Boolean> FD_IS_ONEPRO_ONEPROPER=
        new FeatureDescription<Boolean>(FeatureType.FT_BOOL, "one_pro_one_proper");
	
	public static final FeatureDescription<Boolean> FD_IS_ONETIME_ONENUM=
        new FeatureDescription<Boolean>(FeatureType.FT_BOOL, "one_time_one_num");
	
	public static final FeatureDescription<Boolean> FD_IS_POSMATCH=
        new FeatureDescription<Boolean>(FeatureType.FT_BOOL, "posmatch");
	
	public static final FeatureDescription<Integer> FD_IS_SENT_DIST=
        new FeatureDescription<Integer>(FeatureType.FT_SCALAR, "sentDis");
	
	public static final FeatureDescription<Boolean> FD_IS_ENGandPRO=
        new FeatureDescription<Boolean>(FeatureType.FT_BOOL, "eng&pro");
	
	public static final FeatureDescription<Boolean> FD_IS_ENGandPROPER=
        new FeatureDescription<Boolean>(FeatureType.FT_BOOL, "eng&proper");
	
	@Override
	public void describeFeatures(List<FeatureDescription> fds) {
		fds.add(FD_IS_STRMATCH); 
		fds.add(FD_IS_ONEPRO);
		fds.add(FD_ARE_BOTHPROANDMATCH);
		fds.add(FD_ARE_BOTHPROPER);
		fds.add(FD_IS_ONEPRO_ONEPROPER);
		fds.add(FD_IS_ONETIME_ONENUM);
		fds.add(FD_IS_POSMATCH);
		fds.add(FD_IS_SENT_DIST);
		fds.add(FD_IS_ENGandPRO);
		fds.add(FD_IS_ENGandPROPER);
	}

	@Override
	public void extractFeatures(PairInstance inst) {
		Mention a = inst.getAnaphor();
		Mention b = inst.getAntecedent();
		
		if (a.getExtent().equals(b.getExtent())) 
			inst.setFeature(FD_IS_STRMATCH, true);
		else
			inst.setFeature(FD_IS_STRMATCH, false);
		
		if (a.getPronoun() || b.getPronoun())
			inst.setFeature(FD_IS_ONEPRO, true);
		else
			inst.setFeature(FD_IS_ONEPRO, false);
		
		if (a.getPronoun() && b.getPronoun() && a.getExtent().equals(b.getExtent()))
			inst.setFeature(FD_ARE_BOTHPROANDMATCH, true);
		else
			inst.setFeature(FD_ARE_BOTHPROANDMATCH, false);
		
		if (a.getProperName() && b.getProperName())
			inst.setFeature(FD_ARE_BOTHPROPER, false);
		else
			inst.setFeature(FD_ARE_BOTHPROPER, true);
		
		if ((a.getPronoun()&&b.getProperName())
				|| (b.getPronoun()&&a.getProperName()))
			inst.setFeature(FD_IS_ONEPRO_ONEPROPER, true);
		else
			inst.setFeature(FD_IS_ONEPRO_ONEPROPER, false);
		
		Token head_token_a = a.getdoc().gettoken(a.getHeadEndtokenId());
		Token head_token_b = b.getdoc().gettoken(b.getHeadEndtokenId());
		if (head_token_a.getTag().matches("nt|m") && head_token_b.getTag().matches("nt|m"))
			inst.setFeature(FD_IS_ONETIME_ONENUM, true);
		else
			inst.setFeature(FD_IS_ONETIME_ONENUM, false);
		
		if (head_token_a.getTag().equals(head_token_b.getTag()))
			inst.setFeature(FD_IS_POSMATCH, true);
		else
			inst.setFeature(FD_IS_POSMATCH, false);
		
		inst.setFeature(FD_IS_SENT_DIST, Math.abs(inst.getAnaphor().mention_index
    			-inst.getAntecedent().mention_index));
		
		if (head_token_a.getTag().matches("ws|n") && head_token_b.getTag().matches("n|ws"))
			inst.setFeature(FD_IS_ENGandPRO, true);
		else
			inst.setFeature(FD_IS_ENGandPRO, false);
		
		if ((head_token_a.getTag().equals("ws")&&b.getProperName())
				|| (head_token_b.getTag().equals("ws")&&a.getProperName()))
			inst.setFeature(FD_IS_ENGandPROPER, true);
		else
			inst.setFeature(FD_IS_ENGandPROPER, false);
	}
	
}
